package com.example.t410.service.serviceImpl;
import com.example.t410.dao.LunTan;
import com.example.t410.dao.PingLun;
import com.example.t410.model.PingLunDTO;
import com.example.t410.model.PingLunFindReturn;
import com.example.t410.service.PingLunService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PingLunServiceImpl implements PingLunService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void add(PingLunDTO pingLunDTO) {
        PingLun pingLun = new PingLun();
        BeanUtils.copyProperties(pingLunDTO,pingLun);
        Long l =  System.currentTimeMillis();
        pingLun.setComTime(l);
        pingLun.setLikeNum((int)(l%100));
        mongoTemplate.save(pingLun,"pingLun");
    }

    @Override
    public PingLunFindReturn findById(String id) {
        Criteria criteria = Criteria.where("lunTanId").is(id);
        Query query = new Query(criteria);
        List<PingLun> pingLuns = mongoTemplate.find(query, PingLun.class);
        Criteria criteria2 = Criteria.where("_id").is(id);
        Query query2 = new Query(criteria2);
        List<LunTan> lunTans = mongoTemplate.find(query2, LunTan.class);
        LunTan lunTan = lunTans.get(0);
        return new PingLunFindReturn(pingLuns,lunTan);
    }

    @Override
    public void deleteById(String id) {
        // 创建一个Query来指定删除条件
        Query query = new Query(Criteria.where("_id").is(id));
        // 执行删除操作
        mongoTemplate.remove(query, PingLun.class);
    }
}
